<?php

namespace app\command;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class MqReceive extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('mq_receive');
        // 设置参数
        
    }

    protected function execute(Input $input, Output $output)
    {
    	// 指令输出
    	$output->writeln('mq_receive====秒杀');
        $connect = new AMQPStreamConnection(config("rabbitmq.server"),config("rabbitmq.port"),config("rabbitmq.user"),config("rabbitmq.password"));
        $channel = $connect->channel();
        $queue_name = config("rabbitmq.rabbit_queue");
        $channel->queue_declare($queue_name,false,false,false,false);

        $callbakck = function ($msg){
          $data = json_decode($msg->body,JSON_UNESCAPED_UNICODE);
          $state = Db::table('orders')->insertGetId(["mobile"=>$data["mobile"],"goods_id"=>$data["good_id"]]);
          if($state){
            $msg->ack();
          }
        };

        $channel->basic_consume($queue_name,'',false,false,false,false,$callbakck);
        while ($channel->is_consuming()){
            $channel->wait();
        }
        $channel->close();
        $connect->close();
    }
}
